package org.example.utils;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

import java.util.HashMap;
import java.util.Random;
import java.util.Set;

public class MsgUtil {
    private static final String TO = "18569590667";

    public static String sendMsg() {
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "2c94811c8a27cf2d018aa6252f1d229c";
        String accountToken = "4425ab36c1d9405c8054d76b14b7b547";
        //请使用管理控制台中已创建应用的APPID
        String appId = "2c94811c8a27cf2d018aa625307522a3";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_JSON);
        String templateId = "1";
        Random random = new Random();
        Integer verify = random.nextInt(9000) + 1000;
        String[] datas = {verify + "", "10"};
        String subAppend = "4321";  //可选 扩展码，四位数字 0~9999
        String reqId = "biubiu";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        HashMap<String, Object> result = sdk.sendTemplateSMS(TO,templateId,datas);
//        HashMap<String, Object> result = sdk.sendTemplateSMS(TO, templateId, datas, subAppend, reqId);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            return verify + "";
        } else {
            //异常返回输出错误码和错误信息
            System.err.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
        }
        return "0";
    }

    public static void main(String[] args) {
        System.err.println(sendMsg());
    }
}

